package com.lightning.examples.web.test.service;

import org.expressme.persist.Transaction;
import org.expressme.persist.TransactionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lightning.examples.dao.CategoryDao;
import com.lightning.examples.model.Category;
import com.lightning.examples.web.test.command.MarketCmd;
import com.lightning.persist.DaoAccessor;

@Service
public class MarketService {
	@Autowired
	private DaoAccessor repository;
	@Autowired
	private TransactionManager txManager;

	public void db1(MarketCmd cmd) {
		Category cat = new Category();
		cat.setName("Ŀ¼1");
		cat.setCode("100");
		cat.setPrefixCode("0");
		cat.setState("1");
		int id = repository.getDaoSupport(CategoryDao.class).createCategory(cat);
		System.out.println(id);
		id = repository.getDaoSupport(CategoryDao.class).createCategory(cat);
		System.out.println(id);
	}

	@SuppressWarnings("static-access")
	public void txDb1(MarketCmd cmd) {
		Category cat = new Category();
		cat.setName("Ŀ¼1");
		cat.setCode("101");
		cat.setPrefixCode("0");
		cat.setState("1");
		Transaction tx = null;
		try {
			repository.setAutoCommit(false);
			tx = txManager.beginTransaction();
			int id = repository.getDaoSupport(CategoryDao.class).createCategory(cat);
			System.out.println(id);
			id = repository.getDaoSupport(CategoryDao.class).createCategory(cat);
			System.out.println(id);
			tx.commit();
		} catch (Exception e) {
			if (tx != null) {
				tx.rollback();
			}
			e.printStackTrace();
		} finally {
			repository.cleanAutoCommit();
		}
	}
}
